<?php

namespace App\Listeners\Subscriber;

use Exception;
use App\Events\Study\DeletedEvent;
use Illuminate\Events\Dispatcher;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class StudyEventSubscriber
{
    public function onDeleted(DeletedEvent $event)
    {
        try{
            DB::table('user_study_pivot')->where('study_id',$event->study->id)->delete();
        }catch(Exception $e){
            Log::error($e->getMessage());
        }
    }
    
    public function subscribe(Dispatcher $events)
    {
        $events->listen(
            'App\Events\Study\DeletedEvent',
            'App\Listeners\Subscriber\StudyEventSubscriber@onDeleted'
        );
    }
}
